Repository navigation
fix: match params arrays element by element on delegate mocks - #1072
Merged
Merged
Conversation
Test Results 47 files ± 0 47 suites ±0 1h 51m 18s ⏱️ + 11m 54s Results for commit 42517fa. ± Comparison against base commit 2ae8c68. This pull request removes 22 and adds 34 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
🚀 Benchmark ResultsCompared to Memory
Time
CompleteWorkflowBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) Intel Xeon 6975P-C 2.70GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 MockCreationBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 7763 3.09GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 CompleteUnconfiguredBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 7763 2.60GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 CompleteMethodBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 9V45 4.54GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 CompletePropertyBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 CompleteIndexerBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) INTEL XEON PLATINUM 8573C 3.00GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 CompleteEventBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) Intel Xeon 6975P-C 3.87GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 CallbackBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) AMD EPYC 7763 2.64GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 AsyncBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) INTEL XEON PLATINUM 8573C 3.00GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 SequenceBenchmarks
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon) INTEL XEON PLATINUM 8573C 3.46GHz, 1 CPU, 4 logical and 2 physical cores .NET SDK 11.0.100 [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4 Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15 LaunchCount=1 WarmupCount=10 |
Methods with a `params` parameter get a per-element overload for `Setup` and `Verify`, so `It.Is(1), It.IsAny<int>()` matches the elements one by one. Delegate mocks never emitted it: on a `params T[]` delegate such a call did not compile, and on a `params object[]` delegate the element matchers bound to the overload that takes values and were compared as values, so the setup never applied and the verification never matched. The delegate emitter now adds the same overloads as the method emitter, through the same helpers and with the same priorities: the per-element overload for every arity (also above four parameters), and for a lone `params object[]` the overload without parameters that keeps `Setup().AnyParameters()` for a call without arguments. This covers the setup and verify interfaces, their implementations and the verify monitor. A `params` delegate keeps the classic overloads in union mode as well, so both modes are covered. The whole-array matcher of #1031 still matches the whole array, literal elements still match by value, and the null literals of #1032 keep their meaning; `ParamsObjectArrayTests` now compares the delegates with the methods for all of these calls, including `params string[]`. As on methods, a `null` literal for the array next to a matcher for another parameter (`Setup(It.IsAny<int>(), null)`) is now ambiguous (CS0121) between the whole-array and the per-element overload; it compiled for delegates only since #1032 and only with `OverloadResolutionPriorityAttribute`. `It.IsNull<object[]>()` expresses it unambiguously. ```csharp public delegate int Logger(int level, params object[] args); Logger logger = Logger.CreateMock(); logger.Mock.Setup(It.Is(4), It.IsAny<object>(), It.Is<object>(2)).Returns(1); int result = logger(4, "a", 2); // 1, before: 0 logger.Mock.Verify(It.Is(4), It.IsAny<object>(), It.Is<object>(2)).Once(); ``` Fixes #1045
…utionPriorityAttribute is missing Without the attribute (classic mode on .NET 8, .NET Framework or netstandard without a polyfill) a call that passes no `params` arguments, such as `Setup()`, `Setup().AnyParameters()` or `Setup(It.IsAny<int>())`, was ambiguous between the per-element overload and the overload that takes the array as values, because both apply only in their expanded form. Methods already had this problem, and the per-element overloads made it a new break for delegates. In that case every `params T[]` member now also gets an overload that leaves out the `params` parameter and matches an empty array; an overload in normal form wins over both expanded forms. With the attribute nothing changes, so the generator now passes it to the interface implementations as well. A sibling that could already own that signature suppresses the overload. A failed verification with element matchers printed the matcher array's type name; it now lists the matchers like `[It.IsAny<object>(), 2]`, and the overload without `params` arguments prints `[]`. The element matcher is also built once per verification instead of once per recorded call.
vbreuss
force-pushed
the
fix/params-delegate-element-matchers
branch
from
November 20, 2026 05:01
540f023 to
42517fa
Compare
|
github-actions Bot
added a commit
that referenced
this pull request
Nov 20, 2026
…ment on delegate mocks (#1072) by Valentin Breuß
github-actions Bot
added a commit
that referenced
this pull request
Nov 20, 2026
…ment on delegate mocks (#1072) by Valentin Breuß
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Methods with a
paramsparameter get a per-element overload forSetupandVerify, soIt.Is(1), It.IsAny<int>()matches the elements one by one. Delegate mocks never emitted it: on aparams T[]delegate such a call did not compile, and on aparams object[]delegate the element matchers bound to the overload that takes values and were compared as values, so the setup never applied and the verification never matched.The delegate emitter now adds the same overloads as the method emitter, through the same helpers and with the same priorities: the per-element overload for every arity (also above four parameters), and for a lone
params object[]the overload without parameters that keepsSetup().AnyParameters()for a call without arguments. This covers the setup and verify interfaces, their implementations and the verify monitor, in classic and in union mode (a member with aparamsparameter always has the classic overloads). The whole-array matcher of #1031 still matches the whole array and literal elements still match by value;ParamsObjectArrayTestscompares the delegates with the methods for these calls, includingparams string[].Without
OverloadResolutionPriorityAttribute(classic mode on .NET 8, .NET Framework or netstandard without a polyfill) a call withoutparamsarguments, such asSetup(),Setup().AnyParameters()orSetup(It.IsAny<int>()), would be ambiguous between the per-element overload and the one that takes the array as values. Methods already had that problem, so in that case everyparams T[]member, method or delegate, now also gets an overload that leaves out theparamsparameter and matches an empty array. With the attribute the generated code is unchanged.A failed verification with element matchers now lists them, for example
Invoke(It.IsAny<int>(), [It.IsAny<object>(), 2]), and the overload withoutparamsarguments prints[]. The element matcher is built once per verification instead of once per recorded call.Breaking change, as on methods: on a
params T[]delegate, anullarray literal next to any matcher ornullfor another parameter no longer compiles (CS0121 between the whole-array and the per-element overload), for exampleSetup(It.IsAny<int>(), null),Setup(It.IsAny<int>(), args: null)orSetup(null, null)on(int, params object[]). This also applies to delegates with five or more parameters, with optional parameters, or with anobjectorint?first parameter. UseIt.IsNull<T[]>()for the array instead. WithoutOverloadResolutionPriorityAttribute,nullliterals for the elements are ambiguous as well (Setup(null, null)on a loneparams string[],Setup(It.IsAny<int>(), null, null)); pass typed values such as(string?)nullinstead.paramsparameter have no per-element matcher overload #1045